package com.help10000.rms.common

import android.content.Context
import android.content.Intent
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.util.Log
import com.alibaba.sdk.android.push.MessageReceiver
import com.alibaba.sdk.android.push.notification.CPushMessage
import com.amir.common.beans.PushDataEntity
import com.amir.common.utils.ToastUtil
import com.help10000.rms.config.globalData
import com.help10000.rms.ui.activitys.logins.LoginActivity
import com.help10000.rms.ui.utils.JsonUtils
import me.leolin.shortcutbadger.ShortcutBadger


class MyPushReceiver : MessageReceiver() {
    private val _defaultVoice: String = "default_msg.mp3"
    var player = VoicePlayer()
    var customPlayer = VoicePlayer()
    val REC_TAG = javaClass.name

    /**
     * 推送通知的回调方法
     * @param context
     * @param title
     * @param summary
     * @param extraMap
     */
    public override fun onNotification(
        context: Context,
        title: String,
        summary: String,
        extraMap: Map<String, String>
    ) {
        var json = JsonUtils.getString(extraMap)
        if (json == null)
            return
        handleMessage(context, json)
        // TODO 处理推送通知
        Log.i(REC_TAG, "收到一条推送通知 ： $title, summary:$summary,extra:$json")
    }

    var handler = object : Handler(Looper.getMainLooper()) {
        override fun handleMessage(msg: Message) {
            customPlayer.play(msg.obj.toString());
        }
    }


    fun handleMessage(context: Context, json: String) {
        val fileDescriptor = context.assets.openFd(_defaultVoice)
        player.play(fileDescriptor)
        var push: PushDataEntity? = JsonUtils.getBean(json, PushDataEntity::class.java)
        if (push == null) {
            ToastUtil.show("通知处理出错")
            return
        }
        if (push.Badge > 0) {
            ShortcutBadger.applyCount(context, push.Badge)
        } else if (push.Badge < 0) {
            ShortcutBadger.removeCount(context)
        }
        if (push.PlayVoice?.isEmpty() == false) {
            var msg = handler.obtainMessage(1, push.PlayVoice)
            handler.sendMessageDelayed(msg, 2000)
            //延迟一秒，等提示音播放完
        }
    }

    /**
     * 应用处于前台时通知到达回调。注意:该方法仅对自定义样式通知有效,相关详情请参考https://help.aliyun.com/document_detail/30066.html?spm=5176.product30047.6.620.wjcC87#h3-3-4-basiccustompushnotification-api
     * @param context
     * @param title
     * @param summary
     * @param extraMap
     * @param openType
     * @param openActivity
     * @param openUrl
     */
    override fun onNotificationReceivedInApp(
        context: Context,
        title: String,
        summary: String,
        extraMap: Map<String, String>,
        openType: Int,
        openActivity: String,
        openUrl: String
    ) {
        Log.i(
            REC_TAG,
            "onNotificationReceivedInApp ：  : $title : $summary  $extraMap : $openType : $openActivity : $openUrl"
        )
    }

    /**
     * 推送消息的回调方法
     * @param context
     * @param cPushMessage
     */
    public override fun onMessage(context: Context, cPushMessage: CPushMessage) {
        Log.i(REC_TAG, "收到一条推送消息 ： " + cPushMessage.title + ", content:" + cPushMessage.content)
        handleMessage(context, cPushMessage.content)
    }


    /**
     * 从通知栏打开通知的扩展处理
     * @param context
     * @param title
     * @param summary
     * @param extraMap
     */
    public override fun onNotificationOpened(
        context: Context,
        title: String,
        summary: String,
        extraMap: String
    ) {
     //   ShortcutBadger.removeCount(context)
        Log.i(REC_TAG, "onNotificationOpened ：  : $title : $summary : $extraMap")
        //未登录
        if (globalData.Cmpy == null) {
            var intent = Intent(context, LoginActivity::class.java)
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            context.startActivity(intent)
            return
        }
        PushExtraHelper.handlePush(context, extraMap);

    }

    /**
     * 通知删除回调
     * @param context
     * @param messageId
     */
    public override fun onNotificationRemoved(context: Context, messageId: String) {
        Log.i(REC_TAG, "onNotificationRemoved ： $messageId")
       // ShortcutBadger.removeCount(context)
    }

    /**
     * 无动作通知点击回调。当在后台或阿里云控制台指定的通知动作为无逻辑跳转时,通知点击回调为onNotificationClickedWithNoAction而不是onNotificationOpened
     * @param context
     * @param title
     * @param summary
     * @param extraMap
     */
    override fun onNotificationClickedWithNoAction(
        context: Context,
        title: String,
        summary: String,
        extraMap: String
    ) {
        Log.i(REC_TAG, "onNotificationClickedWithNoAction ：  : $title : $summary : $extraMap")
    }


}